import { defineStore } from 'pinia';
import { Client } from './client';
import { Socket } from 'socket.io-client';
import { Message_File } from './type';

export const useStore = defineStore('store', {
  state: () => ({
    client: null as Client | null,
    userId: '',
    name: '',
    appInit: false,
    privateChatUserInfo: {} as {
      friendId: string;
      friendUserName: string;
      friendName: string;
      createTime: string;
    },
    /**
     * 成功建立私聊连接的用户ID表
     */
    privateChatSet: new Set() as Set<string>,
    localMessages: [] as Array<Message_File>
  }),
  actions: {
    initClient(socket: Socket) {
      this.client = new Client(socket);
      this.client.init();
    },
    setUserId(userId: string) {
      this.client!.setUserId(userId);
      this.userId = userId;
      console.log('设置Userid成功', this.userId);
    },
    setName(name: string) {
      this.client!.setName(name);
      this.name = name;
      console.log(' 设置Name成功', this.name);
    }
  }
});
